package com.lhj.library.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.lhj.library.common.entity.PageResult;
import com.lhj.library.common.enums.IsDeletedFlagEnum;
import com.lhj.library.common.enums.IsDisabledFlagEnum;
import com.lhj.library.common.enums.IsReturnFlagEnum;
import com.lhj.library.domain.convert.UserBoConverter;
import com.lhj.library.domain.entity.UserBO;
import com.lhj.library.domain.service.UserDomainService;
import com.lhj.library.infra.basic.entity.Record;
import com.lhj.library.infra.basic.entity.User;
import com.lhj.library.infra.basic.service.RecordService;
import com.lhj.library.infra.basic.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;


@Service
@Slf4j
public class UserDomainServiceImpl implements UserDomainService {

    @Resource
    private UserService userService;

    @Resource
    private RecordService recordService;


    @Override
    public Boolean register(UserBO userBO) {
        if (log.isInfoEnabled()) {
            log.info("UserController.register.bo:{}", JSON.toJSONString(userBO));
        }
        User user = UserBoConverter.INSTANCE.convertBoToBook(userBO);
        user.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        user.setStatus(IsDisabledFlagEnum.UN_DISABLED.getCode());
        Integer result = userService.insert(user);
        return result > 0;
    }

    @Override
    public Boolean update(UserBO userBO) {
        if (log.isInfoEnabled()) {
            log.info("UserController.update.bo:{}", JSON.toJSONString(userBO));
        }
        User user = UserBoConverter.INSTANCE.convertBoToBook(userBO);
        Integer result = userService.update(user);
        return result > 0;
    }

    @Override
    public Boolean delete(UserBO userBO) {
        if (log.isInfoEnabled()) {
            log.info("UserController.delete.bo:{}", JSON.toJSONString(userBO));
        }
        User user = UserBoConverter.INSTANCE.convertBoToBook(userBO);
        user.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        Integer result = userService.update(user);
        return result > 0;
    }

    @Override
    public Boolean changeStatus(UserBO userBO) {
        if (log.isInfoEnabled()) {
            log.info("UserController.changeStatus.bo:{}", JSON.toJSONString(userBO));
        }
        User user = UserBoConverter.INSTANCE.convertBoToBook(userBO);
        int status = (user.getStatus() == 1) ? IsDisabledFlagEnum.DISABLED.getCode() : IsDisabledFlagEnum.UN_DISABLED.getCode();
        user.setStatus(status);
        Integer result = userService.update(user);
        return result > 0;
    }

    @Override
    public UserBO select(UserBO userBO) {
        if (log.isInfoEnabled()) {
            log.info("UserController.select.bo:{}", JSON.toJSONString(userBO));
        }
        User user = UserBoConverter.INSTANCE.convertBoToBook(userBO);
        User queryUser = userService.queryByCondition(user);
        UserBO queryUserBo = UserBoConverter.INSTANCE.convertUserToBo(queryUser);
        return queryUserBo;

    }

    @Override
    public PageResult<UserBO> selectUserPage(UserBO userBO) {
        if (log.isInfoEnabled()) {
            log.info("UserController.selectUserPage.bo:{}", JSON.toJSONString(userBO));
        }
        PageResult<UserBO> pageResult = new PageResult<>();
        pageResult.setPageNo(userBO.getPageNo());
        pageResult.setPageSize(userBO.getPageSize());
        int start = (userBO.getPageNo() - 1) * userBO.getPageSize();
        User user = UserBoConverter.INSTANCE.convertBoToBook(userBO);
        int count = userService.count(user);
        if (count == 0) {
            return pageResult;
        }
        List<User> userList = userService.selectUserPage(user, start, userBO.getPageSize());
        List<UserBO> userBOS = UserBoConverter.INSTANCE.convertUserToBoList(userList);
        pageResult.setResult(userBOS);
        pageResult.setRecords(userBOS);
        return pageResult;
    }
}
